//
//  UIViewController+Category.m
//  CROSBY_Combine
//
//  Created by Harvey on 2019/1/23.
//  Copyright © 2019 Haley. All rights reserved.
//

#import "UIViewController+Category.h"
#import "ProgressHUD.h"

@implementation UIViewController (Category)

- (void)showChoseLanguageView
{
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:showText(@"选择语言") message:showText(@"温馨提示") preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionC = [UIAlertAction actionWithTitle:showText(@"简体中文") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //中文
        [[NSUserDefaults standardUserDefaults] setObject:@"zh-Hans" forKey:kAppLanguage];
        [[NSUserDefaults standardUserDefaults] setObject:showText(@"简体中文") forKey:kChosenLanguage];
        [[NSUserDefaults standardUserDefaults] synchronize];//同步
        [self endSetLanguate];
    }];
    UIAlertAction *actionE = [UIAlertAction actionWithTitle:showText(@"英语") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {//英语
        [[NSUserDefaults standardUserDefaults] setObject:@"en" forKey:kAppLanguage];
        [[NSUserDefaults standardUserDefaults] setObject:showText(@"英语") forKey:kChosenLanguage];
        [[NSUserDefaults standardUserDefaults] synchronize];//同步
        [self endSetLanguate];
    }];
    UIAlertAction *actionD = [UIAlertAction actionWithTitle:showText(@"德语") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {//英语
        [[NSUserDefaults standardUserDefaults] setObject:@"de" forKey:kAppLanguage];
        [[NSUserDefaults standardUserDefaults] setObject:showText(@"德语") forKey:kChosenLanguage];
        [[NSUserDefaults standardUserDefaults] synchronize];//同步
        [self endSetLanguate];
    }];
    UIAlertAction *actionS = [UIAlertAction actionWithTitle:showText(@"西班牙语") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {//西班牙语
        [[NSUserDefaults standardUserDefaults] setObject:@"es" forKey:kAppLanguage];
        [[NSUserDefaults standardUserDefaults] setObject:showText(@"西班牙语") forKey:kChosenLanguage];
        [[NSUserDefaults standardUserDefaults] synchronize];//同步
        [self endSetLanguate];
    }];
    UIAlertAction *actionP = [UIAlertAction actionWithTitle:showText(@"葡萄牙语") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {//葡萄牙语
        [[NSUserDefaults standardUserDefaults] setObject:@"pt-PT" forKey:kAppLanguage];
        [[NSUserDefaults standardUserDefaults] setObject:showText(@"葡萄牙语") forKey:kChosenLanguage];
        [[NSUserDefaults standardUserDefaults] synchronize];//同步
        [self endSetLanguate];
    }];
    UIAlertAction *actionR = [UIAlertAction actionWithTitle:showText(@"俄语") style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {//俄语
        [[NSUserDefaults standardUserDefaults] setObject:@"ru" forKey:kAppLanguage];
        [[NSUserDefaults standardUserDefaults] setObject:showText(@"俄语") forKey:kChosenLanguage];
        [[NSUserDefaults standardUserDefaults] synchronize];//同步
        [self endSetLanguate];
    }];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:showText(@"取消") style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {//取消
        
    }];
    [alertC addAction:actionC];
    [alertC addAction:actionE];
    [alertC addAction:actionD];
    [alertC addAction:actionS];
    [alertC addAction:actionP];
    [alertC addAction:actionR];
    [alertC addAction:cancelAction];
    [self presentViewController:alertC animated:YES completion:nil];
}

- (void)endSetLanguate
{
    [ProgressHUD show:showText(@"切换语言")];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [[NSNotificationCenter defaultCenter] postNotificationName:kAppLanguageChangedNotification object:nil];
        [ProgressHUD showSuccess:showText(@"切换成功")];
    });
}

@end
